package com.likai.blog.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.likai.blog.pojo.Blogger;
import com.likai.blog.service.IBloggerService;

/**
 * 自定义shiro realm
 * @author likai
 * @since 2018-04-30
 */
public class MyRealm extends AuthorizingRealm {
	
	@Autowired
	@Qualifier("bloggerService")
	private IBloggerService bloggerService ;

	/**
	 * 角色、权限认证(身份认证成功后)
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		return null;
	}

	/**
	 * 身份认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//获取用户信息
		String userName = (String) token.getPrincipal() ;
		//根据用户查询用户信息
		Blogger blogger = bloggerService.findByUserName(userName) ;
		if(blogger != null) {
			//传入用户名与密码由shiro比对
			AuthenticationInfo authenticationInfo = 
					new SimpleAuthenticationInfo(blogger.getUserName(),blogger.getPassword(),"x") ;
			
			return authenticationInfo ;
		} else {
			return null ;
		}
		
		
	}

}
